package com.example.controller;

import com.example.common.Result;
import com.example.entity.Card;
import com.example.service.CardService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import java.util.List;
/**
*  的控制层
**/
@RestController
@RequiredArgsConstructor
@RequestMapping("/card")
public class CardController {

   private final CardService cardService;

   /**
   * 增
   **/
   @PostMapping("/add")
   public Result add(@RequestBody Card card) {
       cardService.add(card);
       return Result.success();
   }

   /**
   * 删
   **/
   @DeleteMapping("/delete/{id}")
   public Result deleteById(@PathVariable Integer id) {
       cardService.deleteById(id);
       return Result.success();
   }

   /**
   * 批量删除
   **/
   @DeleteMapping("/delete/batch")
   public Result deleteBatch(@RequestBody List<Integer> ids) {
       cardService.deleteBatch(ids);
       return Result.success();
   }

   /**
   * 改
   **/
   @PutMapping("/update")
   public Result updateById(@RequestBody Card card) {
       cardService.updateById(card);
       return Result.success();
   }

   /**
   * 根据ID查询
   **/
   @GetMapping("/selectById/{id}")
   public Result selectById(@PathVariable Integer id) {
       Card card = cardService.selectById(id);
       return Result.success(card);
   }

   /**
   * 查询所有
   **/
   @GetMapping("/selectAll")
       public Result selectAll(Card card ) {
       List<Card> list = cardService.selectAll(card);
       return Result.success(list);
   }

   /**
   * 分页查询
   **/
   @GetMapping("/selectPage")
   public Result selectPage(Card card,
   @RequestParam(defaultValue = "1") Integer pageNum,
   @RequestParam(defaultValue = "10") Integer pageSize) {
   PageInfo<Card> page = cardService.selectPage(card, pageNum, pageSize);
       return Result.success(page);
   }

}